package com.apollographql.apollo.internal;

import com.apollographql.apollo.ApolloCall;
import com.apollographql.apollo.ApolloMutationCall;
import com.apollographql.apollo.ApolloPrefetch;
import com.apollographql.apollo.ApolloQueryCall;
import com.apollographql.apollo.ApolloQueryWatcher;
import com.apollographql.apollo.IdleResourceCallback;
import com.apollographql.apollo.api.Mutation;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.OperationName;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.internal.Utils;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public final class ApolloCallTracker {
    private final AtomicInteger activeCallCount = new AtomicInteger();
    private final Map<OperationName, Set<ApolloMutationCall>> activeMutationCalls = new HashMap();
    private final Map<OperationName, Set<ApolloPrefetch>> activePrefetchCalls = new HashMap();
    private final Map<OperationName, Set<ApolloQueryCall>> activeQueryCalls = new HashMap();
    private final Map<OperationName, Set<ApolloQueryWatcher>> activeQueryWatchers = new HashMap();
    private IdleResourceCallback idleResourceCallback;

    /* access modifiers changed from: package-private */
    public void registerCall(ApolloCall call) {
        Utils.checkNotNull(call, "call == null");
        Operation operation = call.operation();
        if (operation instanceof Query) {
            registerQueryCall((ApolloQueryCall) call);
        } else if (operation instanceof Mutation) {
            registerMutationCall((ApolloMutationCall) call);
        } else {
            throw new IllegalArgumentException("Unknown call type");
        }
    }

    /* access modifiers changed from: package-private */
    public void unregisterCall(ApolloCall call) {
        Utils.checkNotNull(call, "call == null");
        Operation operation = call.operation();
        if (operation instanceof Query) {
            unregisterQueryCall((ApolloQueryCall) call);
        } else if (operation instanceof Mutation) {
            unregisterMutationCall((ApolloMutationCall) call);
        } else {
            throw new IllegalArgumentException("Unknown call type");
        }
    }

    /* access modifiers changed from: package-private */
    public void registerPrefetchCall(ApolloPrefetch apolloPrefetch) {
        Utils.checkNotNull(apolloPrefetch, "apolloPrefetch == null");
        registerCall(this.activePrefetchCalls, apolloPrefetch.operation().name(), apolloPrefetch);
        this.activeCallCount.incrementAndGet();
    }

    /* access modifiers changed from: package-private */
    public void unregisterPrefetchCall(ApolloPrefetch apolloPrefetch) {
        Utils.checkNotNull(apolloPrefetch, "apolloPrefetch == null");
        unregisterCall(this.activePrefetchCalls, apolloPrefetch.operation().name(), apolloPrefetch);
        decrementActiveCallCountAndNotify();
    }

    /* access modifiers changed from: package-private */
    public Set<ApolloPrefetch> activePrefetchCalls(OperationName operationName) {
        return activeCalls(this.activePrefetchCalls, operationName);
    }

    /* access modifiers changed from: package-private */
    public void registerQueryCall(ApolloQueryCall apolloQueryCall) {
        Utils.checkNotNull(apolloQueryCall, "apolloQueryCall == null");
        registerCall(this.activeQueryCalls, apolloQueryCall.operation().name(), apolloQueryCall);
        this.activeCallCount.incrementAndGet();
    }

    /* access modifiers changed from: package-private */
    public void unregisterQueryCall(ApolloQueryCall apolloQueryCall) {
        Utils.checkNotNull(apolloQueryCall, "apolloQueryCall == null");
        unregisterCall(this.activeQueryCalls, apolloQueryCall.operation().name(), apolloQueryCall);
        decrementActiveCallCountAndNotify();
    }

    /* access modifiers changed from: package-private */
    public Set<ApolloQueryCall> activeQueryCalls(OperationName operationName) {
        return activeCalls(this.activeQueryCalls, operationName);
    }

    /* access modifiers changed from: package-private */
    public void registerMutationCall(ApolloMutationCall apolloMutationCall) {
        Utils.checkNotNull(apolloMutationCall, "apolloMutationCall == null");
        registerCall(this.activeMutationCalls, apolloMutationCall.operation().name(), apolloMutationCall);
        this.activeCallCount.incrementAndGet();
    }

    /* access modifiers changed from: package-private */
    public void unregisterMutationCall(ApolloMutationCall apolloMutationCall) {
        Utils.checkNotNull(apolloMutationCall, "apolloMutationCall == null");
        unregisterCall(this.activeMutationCalls, apolloMutationCall.operation().name(), apolloMutationCall);
        decrementActiveCallCountAndNotify();
    }

    /* access modifiers changed from: package-private */
    public Set<ApolloMutationCall> activeMutationCalls(OperationName operationName) {
        return activeCalls(this.activeMutationCalls, operationName);
    }

    /* access modifiers changed from: package-private */
    public void registerQueryWatcher(ApolloQueryWatcher queryWatcher) {
        Utils.checkNotNull(queryWatcher, "queryWatcher == null");
        registerCall(this.activeQueryWatchers, queryWatcher.operation().name(), queryWatcher);
    }

    /* access modifiers changed from: package-private */
    public void unregisterQueryWatcher(ApolloQueryWatcher queryWatcher) {
        Utils.checkNotNull(queryWatcher, "queryWatcher == null");
        unregisterCall(this.activeQueryWatchers, queryWatcher.operation().name(), queryWatcher);
    }

    /* access modifiers changed from: package-private */
    public Set<ApolloQueryWatcher> activeQueryWatchers(OperationName operationName) {
        return activeCalls(this.activeQueryWatchers, operationName);
    }

    public synchronized void setIdleResourceCallback(IdleResourceCallback idleResourceCallback2) {
        this.idleResourceCallback = idleResourceCallback2;
    }

    public int activeCallsCount() {
        return this.activeCallCount.get();
    }

    private <CALL> void registerCall(Map<OperationName, Set<CALL>> registry, OperationName operationName, CALL call) {
        synchronized (registry) {
            Set<CALL> calls = registry.get(operationName);
            if (calls == null) {
                calls = new HashSet();
                registry.put(operationName, calls);
            }
            calls.add(call);
        }
    }

    private <CALL> void unregisterCall(Map<OperationName, Set<CALL>> registry, OperationName operationName, CALL call) {
        synchronized (registry) {
            Set<CALL> calls = registry.get(operationName);
            if (calls == null || !calls.remove(call)) {
                throw new AssertionError("Call wasn't registered before");
            } else if (calls.isEmpty()) {
                registry.remove(operationName);
            }
        }
    }

    private <CALL> Set<CALL> activeCalls(Map<OperationName, Set<CALL>> registry, OperationName operationName) {
        Set<CALL> hashSet;
        Utils.checkNotNull(operationName, "operationName == null");
        synchronized (registry) {
            Set<CALL> calls = registry.get(operationName);
            hashSet = calls != null ? new HashSet<>(calls) : Collections.emptySet();
        }
        return hashSet;
    }

    private void decrementActiveCallCountAndNotify() {
        IdleResourceCallback callback;
        if (this.activeCallCount.decrementAndGet() == 0 && (callback = this.idleResourceCallback) != null) {
            callback.onIdle();
        }
    }
}
